package com.example.ricebibackend.controller;

import com.example.ricebibackend.common.errorcode.ControllerErrorCodeConstants;
import com.example.ricebibackend.common.exception.ControllerException;
import com.example.ricebibackend.common.pojo.CommonResult;
import com.example.ricebibackend.common.utils.JacksonUtil;
import com.example.ricebibackend.controller.param.AnalysisParam;
import com.example.ricebibackend.controller.result.AnalysisResult;
import com.example.ricebibackend.service.AnalysisService;
import com.example.ricebibackend.service.dto.AnalysisAskDTO;
import jakarta.annotation.Resource;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

/**
 * 分析控制层代码
 * @author ricejson
 */
@CrossOrigin
@RestController
@RequestMapping("/analysis")
public class AnalysisController {

    private static final Logger logger = LoggerFactory.getLogger(AnalysisController.class);

    @Resource
    private AnalysisService analysisService;

    @PostMapping("/ask")
    public CommonResult<AnalysisResult> analysis(AnalysisParam param, @RequestPart("file")MultipartFile file) {
        // 打日志
        logger.info("AnalysisController analysis, 接收到参数:{}", JacksonUtil.writeValueAsString(param));
        // 简单参数校验
        if (param == null) {
            throw new ControllerException(ControllerErrorCodeConstants.ANALYSIS_PARAM_NULL);
        }
        // 进行分析转换
        AnalysisAskDTO analysisAskDTO = analysisService.analysisAsk(param, file);
        AnalysisResult result = convertToAnalysisResult(analysisAskDTO);
        return CommonResult.success(result);
    }

    @PostMapping("/ask/async")
    public CommonResult<AnalysisResult> analysisAsync(AnalysisParam param, @RequestPart("file")MultipartFile file) {
        // 打日志
        logger.info("AnalysisController analysisAsync, 接收到参数:{}", JacksonUtil.writeValueAsString(param));
        // 简单参数校验
        if (param == null) {
            throw new ControllerException(ControllerErrorCodeConstants.ANALYSIS_PARAM_NULL);
        }
        // 进行分析转换
        AnalysisAskDTO analysisAskDTO = analysisService.analysisAskAsync(param, file);
        AnalysisResult result = convertToAnalysisResultAsync(analysisAskDTO);
        return CommonResult.success(result);
    }

    /**
     * 异步转换分析结果
     * @param analysisAskDTO 数据传输对象
     * @return
     */
    private AnalysisResult convertToAnalysisResultAsync(AnalysisAskDTO analysisAskDTO) {
        if (analysisAskDTO == null) {
            throw new ControllerException(ControllerErrorCodeConstants.ANALYSIS_ASK_DTO_CONVERT_ERROR);
        }
        AnalysisResult result = new AnalysisResult();
        result.setData(analysisAskDTO.getData());
        result.setResult(analysisAskDTO.getResult());
        return result;
    }

    /**
     * 转换分析结果
     * @param analysisAskDTO 数据传输对象
     * @return
     */
    private AnalysisResult convertToAnalysisResult(AnalysisAskDTO analysisAskDTO) {
        if (analysisAskDTO == null || analysisAskDTO.getData() == null) {
            throw new ControllerException(ControllerErrorCodeConstants.ANALYSIS_ASK_DTO_CONVERT_ERROR);
        }
        AnalysisResult result = new AnalysisResult();
        result.setData(analysisAskDTO.getData());
        result.setResult(analysisAskDTO.getResult());
        return result;
    }
}
